package Queue;

public class MyQueue {
        public static class ListNode {
            ListNode prev;
            ListNode next;
            int value;

            ListNode(int val) {
                this.value = val;
            }
        }


        ListNode first;
        ListNode tail;
        int size = 0;

        public void offer(int val) {
            ListNode newNode = new ListNode(val);
            if(first == null) {
                first = newNode;
            } else {
                tail.next = newNode;
                newNode.prev = tail;
            }

            tail = newNode;
            size++;
        }


        public int poll() {
            int value = 0;
            if(first == null) {
                return -1;
            } else if(first == tail) {
                first = null;
                tail = null;
            } else {
                value = first.value;
                first = first.next;
                first.prev.next = null;
                first.prev = null;
            }
            size--;
            return value;
        }

        public int peek() {
            if(first == null) {
                return -1;
            }

            return first.value;
        }

        public int size() {
            return size;
        }

        public boolean isEmpty() {
            return size() == 0;
        }
}
